package com.wanmait.employment.config;

import com.wanmait.employment.interceptor.JWTInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Resource
    private JWTInterceptor jwtInterceptor;

    //统一配置跨越请求
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
//                .allowedOrigins("*")//允许那些域来访问。allowedOrigins
                .allowedOriginPatterns("*")
                .allowCredentials(true) //如果需要传递cookie需要设置true
                .allowedMethods("*")
                .allowedHeaders("*")
                .maxAge(3600);//3600秒 不用再次发送预检请求 OPTIONS
    }

    //设置静态资源的路径
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/uploadImages/**").addResourceLocations("classpath:/static/uploadImages/");
        //registry.addResourceHandler("/uploadImages/**").addResourceLocations("file:D:/uploadImages/");
        //registry.addResourceHandler("/uploadImages/**").addResourceLocations("file:D:\\uploadImages\\");
    }
    //拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(jwtInterceptor).addPathPatterns("/manage/**").excludePathPatterns("/manage/login","/manage/getCode","/manage/checkCode");

    }
}
